package com.sch.wanandroid.ui.regist

import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.coder.zzq.smartshow.toast.SmartToast
import com.sch.wanandroid.R
import com.sch.wanandroid.base.BaseActivity
import com.sch.wanandroid.databinding.ActivityRegistBinding

/**
 * Created by Sch.
 * Date: 2020/4/24
 * description:注册
 */
class RegistActivity : BaseActivity() {
    private lateinit var registViewModel: RegistViewModel
    private lateinit var databinding: ActivityRegistBinding

    /**
     * 密码是否显示明文
     */
    private var isPasswordShow = false

    /**
     * 确认密码是否显示明文
     */
    private var isRePasswordShow = false

    override fun init(savedInstanceState: Bundle?) {
        initTitle("注册")
        registViewModel = ViewModelProvider(this).get(RegistViewModel::class.java)
        databinding.data = registViewModel

        registViewModel.registSuccessLiveData.observe(this) {
            if (it) {
                registerSuccess()
            }
        }
        registViewModel.errorMsg.observe(this) {
            onError(it)
        }
        initListener()

    }

    private fun initListener() {
        databinding.ivClearUserName.setOnClickListener {
            registViewModel.userName.value = ""
        }
        databinding.ivPasswordVisibility.setOnClickListener {
            databinding.etPassword.requestFocus()
            if (isPasswordShow) {
                isPasswordShow = false
                databinding.ivPasswordVisibility.setImageResource(R.mipmap.password_hide)
                databinding.etPassword.transformationMethod =
                    PasswordTransformationMethod.getInstance()
            } else {
                isPasswordShow = true
                databinding.ivPasswordVisibility.setImageResource(R.mipmap.password_show)
                databinding.etPassword.transformationMethod =
                    HideReturnsTransformationMethod.getInstance()
            }
            databinding.etPassword.setSelection(registViewModel.passWord.value!!.length)
        }
        databinding.ivRePasswordVisibility.setOnClickListener {
            databinding.etRePassword.requestFocus()
            if (isRePasswordShow) {
                isRePasswordShow = false
                databinding.ivRePasswordVisibility.setImageResource(R.mipmap.password_hide)
                databinding.etRePassword.transformationMethod =
                    PasswordTransformationMethod.getInstance()
            } else {
                isRePasswordShow = true
                databinding.ivRePasswordVisibility.setImageResource(R.mipmap.password_show)
                databinding.etRePassword.transformationMethod =
                    HideReturnsTransformationMethod.getInstance()
            }
            databinding.etRePassword.setSelection(registViewModel.rePassword.value!!.length)
        }
        databinding.llRegister.setOnClickListener {
            when {
                registViewModel.userName.value.isNullOrEmpty() -> databinding.etUsername.error =
                    "账号不能为空"
                registViewModel.passWord.value.isNullOrEmpty() -> databinding.etPassword.error =
                    "密码不能为空"
                registViewModel.rePassword.value.isNullOrEmpty() -> databinding.etRePassword.error =
                    "请再次输入密码"
                else -> {
                    setViewStatus(false)
                    registViewModel.doRegister()
                }
            }
        }
    }

    override fun getLayoutId(): Int {
        databinding = DataBindingUtil.setContentView(this, R.layout.activity_regist)
        databinding.lifecycleOwner = this
        return 0
    }

    /**
     * 注册时给具备点击事件的View上锁，失败时解锁
     * 并且施加动画
     */
    private fun setViewStatus(lockStatus: Boolean) {
        databinding.llRegister.isEnabled = lockStatus
        databinding.etUsername.isEnabled = lockStatus
        databinding.etPassword.isEnabled = lockStatus
        databinding.etRePassword.isEnabled = lockStatus
        if (lockStatus) {
            databinding.tvRegisterTxt.visibility = View.VISIBLE
            databinding.indicatorView.visibility = View.GONE
            databinding.indicatorView.hide()
        } else {
            databinding.tvRegisterTxt.visibility = View.GONE
            databinding.indicatorView.visibility = View.VISIBLE
            databinding.indicatorView.show()
        }
    }

    private fun registerSuccess() {
        SmartToast.success("注册成功")
        finish()
    }

    fun onError(ex: String?) {
        setViewStatus(true)
        SmartToast.error(ex)
    }

}